package io.github.talelin.latticy.controller.v1;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.bean.Code;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.Logger;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.dto.BannerDTO;
import io.github.talelin.latticy.mapper.BannerMapper;
import io.github.talelin.latticy.model.BannerDO;
import io.github.talelin.latticy.service.BannerService;
import io.github.talelin.latticy.vo.BannerWithItemsBO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;

/**
 * @ClassName BannerController
 * @Description TODO
 * @Author wushaopei
 * @Date 2021/10/17 19:34
 * @Version 1.0
 */
@RestController
@RequestMapping("/v1/banner")
@Validated
@PermissionModule(value = "Banner")
public class BannerController {

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private BannerService bannerService;


    @PostMapping
    @PermissionMeta(value = "创建Banner")
    @GroupRequired
    public void create(@RequestBody @Validated BannerDTO dto){
        BannerDO bannerDO = new BannerDO();
        BeanUtils.copyProperties(dto,bannerDO);
        bannerService.save(bannerDO);
    }

    @PutMapping("/{id}")
    @PermissionMeta(value = "更新Banner")
    @GroupRequired
    public UpdatedVO update(@RequestBody @Validated BannerDTO dto,
                       @PathVariable @Positive Long id){
        bannerService.update(dto,id);
        return new UpdatedVO(2, Code.UPDATED.getZhDescription());
    }

    @GetMapping("/{id}")
    @PermissionMeta(value = "查询Banner")
    @Logger(template = "{user.username}查询了Banner数据")
    @GroupRequired
    public BannerWithItemsBO getWithItems(@PathVariable @Positive Long id){
        return bannerService.getWithItem(id);
    }

    @DeleteMapping("/{id}")
    @PermissionMeta(value = "删除Banner")
    @GroupRequired
    public DeletedVO delete(@PathVariable @Positive Long id){
        bannerService.delete(id);
        return new DeletedVO(3,Code.DELETED.getZhDescription());
    }
    @GetMapping("/pages")
    public PageResponseVO<BannerDO> getBanners(@RequestParam(required = false,defaultValue = "0")
                                         @Min(value = 0) Integer page,// 页码
                                               @RequestParam(required = false,defaultValue = "50")
                                         @Min(value = 1) @Max(value = 20) Integer count){ // 数据量
        // 使用自定义的Page类，组织分页数据
        Page<BannerDO> pager = new Page<>(page,count);
        // 执行查询返回分页对象
        IPage<BannerDO> paging = bannerService.getBaseMapper().selectPage(pager,null);
        int a = 1;
        // 不能直接返回分页对象，需要处理成json结构。不需要返回的参数使用@JsonIgnore注解标识
        return new PageResponseVO<>(paging.getTotal(),paging.getRecords(),paging.getCurrent(),paging.getSize());
    }
}
